package cn.broadsense.biz.handler;

import cn.broadsense.common.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author: pengxin
 * @create: 2023-09-13 16:59
 * @Version 1.0
 **/
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        String userName ="";
        try{
            userName  = SecurityUtils.getUsername();
        }catch (Exception e){

        }
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateBy", String.class, userName);
        this.strictInsertFill(metaObject, "createBy", String.class, userName);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        String userName ="";
        try{
            userName  = SecurityUtils.getUsername();
        }catch (Exception e){

        }
        this.strictUpdateFill(metaObject, "updateBy", String.class, userName);
    }
}
